package com.dormitory.dormitory_management.controller;

import com.dormitory.dormitory_management.common.MessageConstant;
import com.dormitory.dormitory_management.common.PageResult;
import com.dormitory.dormitory_management.common.Result;
import com.dormitory.dormitory_management.common.StatusCode;
import com.dormitory.dormitory_management.domain.StudentDorm;
import com.dormitory.dormitory_management.domain.UserInfo;
import com.dormitory.dormitory_management.service.DormitoryService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dormitory")
public class DormitoryController {

    @Autowired
    private DormitoryService dormitoryService;

    @RequestMapping("/login")
    public int login(@RequestBody UserInfo uinfo) throws IOException {
        int flag=-1;
        flag=dormitoryService.login(uinfo.uid,uinfo.passWord);
        return flag;//1：用户名不存在    2：密码不对  3: 学生   4：管理员
    }
    @RequestMapping("/CountNum")//查询总宿舍数
    public int CountNum() throws IOException {
        int num=-1;
        num=dormitoryService.CountNum();
        return num;
    }
    @RequestMapping("/find")


    public Result find()
    {
        List<StudentDorm> all = dormitoryService.findAll();

        return new Result(false,200,"请求成功4534534",all);
    }

    @RequestMapping("/search")
    public PageResult search(@RequestBody Map searchMap)
    {
        Page<StudentDorm> page = dormitoryService.search(searchMap);
        return new PageResult(true,10,"success!",page.getResult(),page.getTotal());
    }
    @RequestMapping("/add")
    public Result add(@RequestBody StudentDorm stuDorm){
        //System.out.println("方法是add");
        Boolean add = dormitoryService.add(stuDorm);
        return new Result(add, StatusCode.OK, MessageConstant.COMMUNITY_ADD_SUCCESS);
    }
    @RequestMapping("/findById")
    public Result findById(Integer did){
        StudentDorm dormitory = dormitoryService.findById(did);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_FIND_BY_ID_SUCCESS,dormitory);
    }
    @RequestMapping("/count")
    public boolean count(Integer did) throws IOException {
        //System.out.println("id是"+did);
        boolean count;
        count= dormitoryService.count(did);
        //System.out.println("count是"+count);
        return count;
    }

    @RequestMapping("/update")
    public Result update(@RequestBody StudentDorm dormitory){
        System.out.println("方法是update");
        Boolean add = dormitoryService.update(dormitory);
        return new Result(add,StatusCode.OK,MessageConstant.COMMUNITY_UPDATE_SUCCESS);
    }


    @RequestMapping("/del")
    public  Result del(@RequestBody List<Integer> did){

        Boolean del = dormitoryService.del(did);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_DELETE_SUCCESS);
    }

}
